package com.toycloud.common.excel.converter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.fastjson2.JSONObject;


import java.util.List;

public class ListToJSONStringConverter implements Converter<List> {
    @Override
    public Class<List> supportJavaTypeKey() {
        return List.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(List value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        // 对空数组或 null 做处理
        if (value == null || value.isEmpty()) {
            // 返回空 JSON 数组字符串 "[]"
            return new WriteCellData<>("[]");
        }
        // 将 List<JSONObject> 转换为 JSON 字符串
        return new WriteCellData<>(JSONObject.toJSONString(value));
    }
}

